﻿using System;
using System.Web;

namespace FBGraph.Web.Security
{
    /// <summary>An implementation of <see cref="FBGraphHttpHandler" /> that handles authorize requests for WebForms applications.</summary>
    public class FBGraphAuthorizeRequestHandler : FBGraphHttpHandler
    {
        internal const String PATH = "AUTHORIZE";

        /// <summary>Attempts to authorize the user and redirects them if they are not authorized.</summary>
        protected override void ProcessRequest()
        {
            var strScope = this.Request.QueryString["scope"];
            var scope = default(ExtendedPermission);
            if (!String.IsNullOrEmpty(strScope))
            {
                scope = (ExtendedPermission)Enum.Parse(typeof(ExtendedPermission), strScope);
            }

            var postAuthorizeUrl = String.Format("{0}{1}", this.AuthUrl, FBGraphPostAuthorizeRequestHandler.PATH.ToLower());
            var redirectUrl = this.FbContext.PrepareAuthUrl(postAuthorizeUrl, this.ReturnUrl, scope, false);

            this.HttpContext.Response.Redirect(redirectUrl);
        }
    }
}